/*
 *     This file is part of Neptune Network Emulator.
 *
 *  Neptune Network Emulator is free software: 
 *  you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Neptune Network Emulator is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Neptune Network Emulator.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/**
 *
 */
package neptune.web.client.view.components.dialogs;

import neptune.web.client.WebInterface;
import neptune.web.client.model.topology.SerializableTopology;
import neptune.web.client.services.experiment.TemplateService;
import neptune.web.client.view.components.MessageWidget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
 *
 */
public class SaveAsTemplateDialog extends DialogBox implements ClickListener
{

	private TextBox textTemplateName;
	private TextBox textTemplateDescription;
	private SerializableTopology serTopology;
	
	public SaveAsTemplateDialog(SerializableTopology serTopology)
	{
		super(false, true);
		this.serTopology=serTopology;
		FlexTable mainPanel = new FlexTable();
		mainPanel.setWidth("95%");
		ScrollPanel mainPanelWrapper = new ScrollPanel(mainPanel);
		mainPanelWrapper.setHeight("130px");

		/*
		 * Inizio costruzione campi per inserimemnto dati
		 */
		// Name
		Label labelName = new Label("Template name");
		labelName.setStylePrimaryName("NodePropsDialog-label");
		this.textTemplateName = new TextBox();
		this.textTemplateName.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(0, 0, labelName);
		mainPanel.setWidget(0, 1, this.textTemplateName);
		
		// DESCRIPTION
		Label labelDescription = new Label("Description");
		labelDescription.setStylePrimaryName("NodePropsDialog-label");
		this.textTemplateDescription = new TextBox();
		this.textTemplateDescription.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(1, 0, labelDescription);
		mainPanel.setWidget(1, 1, this.textTemplateDescription);
		/*
		// IP
		Label labelIP = new Label("Ip address");
		labelIP.setStylePrimaryName("NodePropsDialog-label");
		this.textIpAddress = new TextBox();
		this.textIpAddress.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(3, 0, labelIP);
		mainPanel.setWidget(3, 1, this.textIpAddress );

		// root password
		Label labelPasswd = new Label("Root passwd");
		labelPasswd.setStylePrimaryName("NodePropsDialog-label");
		this.textRootPasswd = new PasswordTextBox();
		this.textRootPasswd.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(4, 0, labelPasswd);
		mainPanel.setWidget(4, 1, this.textRootPasswd);
		*/
		
		// BUTTON PANEL
		HorizontalPanel buttonPanel = new HorizontalPanel();
		Button ok = new Button("Save");
		ok.addClickListener(this);
		ok.setStylePrimaryName("NodePropsDialog-button");
		Button cancel = new Button("Cancel");
		cancel.addClickListener(this);
		cancel.setStylePrimaryName("NodePropsDialog-button");
		buttonPanel.add(ok);
		buttonPanel.add(cancel);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.add(mainPanelWrapper);
		vPanel.add(buttonPanel);
		vPanel.setCellHorizontalAlignment(buttonPanel, VerticalPanel.ALIGN_RIGHT);

		this.setWidget(vPanel);
		this.setText("Save as template");

		// Styles
		mainPanelWrapper.setStylePrimaryName("NodePropsDialog-mainWrapper");
		mainPanel.setStylePrimaryName("NodePropsDialog-mainPanel");
		this.setStylePrimaryName("NodePropsDialog");
	}
	
	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
	 */
	public void onClick(Widget sender)
	{
		if( ((Button) sender).getText().equals("Save") )
		{
			if ( this.isInputDataValid() )
			{
				//TODO
				this.serTopology.setName(this.textTemplateName.getText());
				this.serTopology.setDescription(this.textTemplateDescription.getText());
				
				String message = "Saving template ...";
				final PopupPanel popup = WebInterface.showMessageDialog(
						new MessageWidget(MessageWidget.LOADING, message), false);
				
				TemplateService.Util.getInstance().saveTemplate(this.serTopology, new AsyncCallback<Boolean>()
				{

					public void onFailure(Throwable caught)
					{
						popup.hide();
		            	String message=new String("Error cause: "+caught.getMessage() );
		            	WebInterface.addMessageToConsoleAndShowDialog(message,true);
					}

					public void onSuccess(Boolean result) 
					{
						popup.hide();
		            	String message=new String("Template added!");
		            	WebInterface.addMessageToConsoleAndShowDialog(message,false);
					}
				});
				//this.serTopology.set
				this.hide();
				resetWindow();
			}
			else
			{
				// I DATI INSERITI NON SONO VALIDI
			}
		}
		else if ( ((Button) sender).getText().equals("Cancel") )
		{
			// L'utente ha premuto Cancel
			this.hide();
			resetWindow();
		}
		else
		{
			
		}		
	}
	
	private void resetWindow()
	{
		this.textTemplateName.setText("");
	}
	
	private boolean isInputDataValid()
	{
		//TODO
		return true;
	}

}
